package com.malie.myspringboot.rabbitmq.springbootRabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {


    public static final String EXCHANGE_DIRECT_HUANG = "direct.huang";//direct类型交换机
    public static final String EXCHANGE_FANOUT_HUANG = "fanout.huang";//fanout类型交换机
    public static final String EXCHANGE_TOPIC_HUANG = "topic.huang";//fanout类型交换机
    public static final String QUEUE_1 = "queue1";
    public static final String QUEUE_2 = "queue2";
    public static final String QUEUE_3 = "queue3";
    public static final String ROUTINGKEY_DIRECT_HUANG = "huang";


    //声明交换机
    @Bean(EXCHANGE_FANOUT_HUANG)
    public FanoutExchange fanoutExchange() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.fanoutExchange(EXCHANGE_FANOUT_HUANG).durable(true).build();
    }

    //声明队列
    /*
     *   new Queue(QUEUE_EMAIL,true,false,false)
     *   durable="true" 持久化 rabbitmq重启的时候不需要创建新的队列
     *   auto-delete 表示消息队列没有在使用时将被自动删除 默认是false
     *   exclusive  表示该消息队列是否只在当前connection生效,默认是false
     */
    @Bean(QUEUE_1)
    public Queue queue1() {
        return new Queue(QUEUE_1, false, false, false);
    }

    @Bean(QUEUE_2)
    public Queue queue2() {
        return new Queue(QUEUE_2, false, false, false);
    }

    @Bean
    public Binding bindingFANOUT() {
        return BindingBuilder.bind(queue1()).to(fanoutExchange());
    }

    @Bean
    public Binding bindingFANOUT2() {
        return BindingBuilder.bind(queue2()).to(fanoutExchange());
    }
}
